import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

class Solution {
    public long[] distance(int[] nums) {
        int len = nums.length;
        long[] arr = new long[len];
        HashMap<Integer, List<Integer>> map = new HashMap<>();
        for(int i = 0;i<len;i++) {
            if(!map.containsKey(nums[i])) {
                List<Integer> list = new ArrayList<>();
                list.add(i);
                map.put(nums[i],list);
            } else {
                List<Integer> list = map.get(nums[i]);
                list.add(i);
            }
        }
        for(int i = 0;i<len;i++) {
            List<Integer> list = map.get(nums[i]);
            int tmp = i;
            for(Integer num:list) {
                tmp += Math.abs(i-num);
            }
            arr[i] = tmp;
        }
        return arr;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1,3,1,1,2};
        solution.distance(nums);
    }
}